package com.yang.flower.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yang.flower.common.Constants;
import com.yang.flower.common.Result;
import com.yang.flower.entity.Dict;
import com.yang.flower.entity.Menu;
import com.yang.flower.entity.Role;
import com.yang.flower.entity.RoleMenu;
import com.yang.flower.mapper.RoleMenuMapper;
import com.yang.flower.service.DictService;
import com.yang.flower.service.RoleService;
import com.yang.flower.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.yang.flower.service.MenuService;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 菜单控制器
 * @Author: Yang
 * @DateTime: 2022/12/9 17:06
 **/
@RestController
@CrossOrigin
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    @Autowired
    private DictService dictService;

    @Autowired
    private RoleService roleService;

    @Resource
    private RoleMenuMapper roleMenuMapper;

    //分页查询菜单
    @GetMapping("/page")
    public Page<Menu> page(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam String name){
        Page<Menu> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Menu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Menu::getName,name);
        queryWrapper.orderByDesc(Menu::getId);
        Page<Menu> pageInfo = menuService.page(page, queryWrapper);
        return pageInfo;
    }

    @GetMapping
    public Result findAll(@RequestParam(defaultValue = "") String name){
        List<Menu> menus = menuService.findMenus(name);
        return Result.success(menus);
    }

    //添加菜单
    @PostMapping("/add")
    public Result add(@RequestBody Menu menu){
        return Result.success(menuService.save(menu));
    }

    //修改菜单信息
    @PutMapping("/edit")
    public Result edit(@RequestBody Menu menu){
        return Result.success(menuService.saveOrUpdate(menu));
    }

    //根据id删除指定的菜单信息
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        return Result.success(menuService.removeById(id));
    }

    //根据所勾选,批量删除菜单信息
    @DeleteMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody List<String> ids){
        return Result.success(menuService.removeByIds(ids));
    }

    @GetMapping("/icons")
    public Result getIcons(){
        QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", Constants.DICT_TYPE_ICON);
        return Result.success(dictService.list(queryWrapper));
    }

}
